//
//  UIScrollView+Addition.m
//  YRCommon
//
//  Created by FengHan on 25/07/13.
//  Copyright (c) 2013 zzh. All rights reserved.
//

#import "UIScrollView+Addition.h"

@interface UIView (ScrollsToTopRecursively)

@end

@implementation UIView (ScrollsToTopRecursively)

- (void)_setAllSubScrollViewScrollsToTopRecursively:(BOOL)top
{
    for (UIView *v in self.subviews) {
        if ([v isKindOfClass:[UIScrollView class]]) {
            [(UIScrollView *)v setScrollsToTop:top];
        }
        [v _setAllSubScrollViewScrollsToTopRecursively:top];
    }
}

@end

@implementation UIScrollView (Addition)

- (void)killScroll
{
    if (self.isDecelerating == NO) return;

    CGPoint offset = self.contentOffset;
    offset.x -= 1.0;
    offset.y -= 1.0;
    [self setContentOffset:offset animated:NO];
    offset.x += 1.0;
    offset.y += 1.0;
    [self setContentOffset:offset animated:NO];
}

- (void)tryScrollsToTop
{
    [self.window _setAllSubScrollViewScrollsToTopRecursively:NO];
    self.scrollsToTop = YES;
}

- (void)scrollTop:(BOOL)animated
{
    CGPoint topOffset = CGPointMake(self.contentOffset.x, -self.contentInset.top);
    [self setContentOffset:topOffset animated:animated];
}

- (void)scrollBottom:(BOOL)animated
{
    if (self.contentSize.height > self.bounds.size.height - self.contentInset.top - self.contentInset.bottom) {
        CGPoint bottomOffset = CGPointMake(self.contentOffset.x, self.contentSize.height + self.contentInset.bottom - self.bounds.size.height);
        [self setContentOffset:bottomOffset animated:animated];
    }
}

- (BOOL)isScrollBottom
{
    return self.contentOffset.y >= (self.contentSize.height + self.contentInset.bottom - self.bounds.size.height)-40;
}

@end
